package com.leyantech.ecp.agenx.session.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Modifier;
import java.util.List;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-03.
 */
@Getter
@Builder
@EqualsAndHashCode(of = {"storeId", "assistantId"})
public class Assistant {

  public static Gson gson = new GsonBuilder()
      .excludeFieldsWithModifiers(Modifier.STATIC)
      .create();

  /**
   * 店铺号.
   */
  @SerializedName("store_id")
  private final String storeId;

  /**
   * 客服号.
   */
  @SerializedName("assistant_id")
  private final String assistantId;

  /**
   * 客服昵称.
   */
  @SerializedName("assistant_name")
  private final String assistantName;

  /**
   * 客户端唯一 id.
   */
  @SerializedName("client_uuid")
  private final String clientUuid;

  /**
   * 客户端版本号.
   */
  @SerializedName("client_version")
  private final String clientVersion;

  /**
   * 模式列表.
   */
  @SerializedName("functions")
  @Setter
  private List<String> functionList;

  /**
   * 服务模式.
   */
  @SerializedName("service_mode")
  @Setter
  private String serviceMode;

  /**
   * 拓展数据.
   */
  @Default
  @SerializedName("extension")
  private final JsonObject extension = new JsonObject();

  /**
   * 动作执行的时间，毫秒.
   */
  @Setter
  @SerializedName("timestamp")
  private long timestamp;

  /**
   * 客服最后的动作，LOGIN/LOGOUT/CONFIG_CHANGED.
   */
  @SerializedName("action")
  @Setter
  @Default
  private String action = "undefined";

  /**
   * 服务状态，SERVING, NON_SERVING.
   */
  @SerializedName("status")
  @Setter
  private String status;

  /**
   * 所在节点 id, 主要做停机重启，通知清除缓存.
   */
  @Setter
  @SerializedName("node_id")
  private String nodeId;

  public boolean isLogin() {
    return "LOGIN".equals(action);
  }

  public boolean isLogout() {
    return "LOGOUT".equals(action);
  }

  /**
   * 客服所在服务器停机、重启.
   */
  public boolean isShutdown() {
    return "SHUTDOWN".equals(action);
  }

  @Override
  public String toString() {
    return gson.toJson(this);
  }

  public static Assistant fromJson(String json) {
    return gson.fromJson(json, Assistant.class);
  }

}
